<?php
/* SVN FILE: $Id: category.php 238 2010-03-06 11:35:39Z miha.nahtigal $ */
/**
 * Short description for category.php
 *
 * Long description for category.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil
 * @subpackage    lil.models
 * @since         v 1.0
 * @version       $Revision: 238 $
 * @modifiedby    $LastChangedBy: miha.nahtigal $
 * @lastmodified  $Date: 2010-03-06 12:35:39 +0100 (sob, 06 mar 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Category class
 *
 * @uses          LilAppModel
 * @package       lil
 * @subpackage    lil.models
 */
class Category extends LilBlogsAppModel {
/**
 * name property
 *
 * @var string 'Category'
 * @access public
 */
	var $name = 'Category';
/**
 * actsAs property
 *
 * @var array
 * @access public
 */
	var $actsAs = array('Containable', 'Tree');
/**
 * hasMany property
 *
 * @var array
 * @access public
 */
	var $hasAndBelongsToMany = array(
		'Post' => array(
			'className'  => 'LilBlogs.Post',
			'conditions' => array('Post.status >' => 0)
		)
	); 
/**
 * belongsTo property
 *
 * @var array
 * @access public
 */
	var $belongsTo = array(
		'Parent' => array(
			'className'  => 'LilBlogs.Category',
			'foreignKey' => 'parent_id'
		)
	);
/**
 * validate property
 *
 * @var array
 * @access public
 */
	var $validate = array(
		'name' => array(
			'rule' => array('minLength', '1'),
			'required' => true
		),
	);
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @access private
 * @return void
 */
	function __construct($id = false, $table = null, $ds = null) {
		if (!Configure::read('Lil.noAreas') && ($blog_assoc = Configure::read('LilBlogs.blogAssociation'))) {
			$this->belongsTo['Blog'] = $blog_assoc;
		}
		
		parent::__construct($id, $table, $ds);
	}
/**
 * beforeSave callback
 *
 * @access public
 * @return boolean
 */
	function beforeSave() {
		if (isset($this->data[$this->alias]['blog_id']) && empty($this->data[$this->alias]['blog_id'])) {
			$this->data[$this->alias]['blog_id'] = null;
		}
		return true;
	}
/**
 * generateTree method
 *
 * create array for Category selection
 *
 * @param string $r
 * @param array $options
 * @access public
 * @return array Two different arrays; first for select element, second for tree with js
 */
	function generateTree($r = 'both', $options = array()) {
		$defaults = array(
			'path_separator' => ' | ',
			'order'          => 'Category.lft',
			'recursive'      => -1
		);
		$options = array_merge($defaults, $options);
		
		$stack = array(); $p = array(); 
		$levels = array(); $path = array();
		
		$categories = $this->find('all', $options);
		
		foreach ($categories as $category) {
			while ($stack && ($stack[count($stack) - 1] < $category['Category']['rght'])) array_pop($stack);
			while (count($levels) > count($stack)) array_pop($levels);
			
			// set path
			$pro = array(
				'id'       => $category['Category']['id'],
				'name'     => $category['Category']['name'],
				'children' => array(),
				'level'    => count($stack)
			);
			
			$title_var = array();
			$current_var = &$path;
			foreach ($levels as $level) { 
				$title_var[] = $current_var[$level]['name'];
				$current_var = &$current_var['children'];
			}
			
			$title_var[] = $pro['name'];
			$current_var[$category['Category']['id']] = $pro;
			
		 	$levels[count($stack)] = $category['Category']['id'];
		 	
		 	$stack[] = $category['Category']['rght'];
			$p[$category['Category']['id']] = implode($options['path_separator'], Set::filter($title_var));
		}
		
		$ret = array('simple' => $p, 'threaded' => $path);
		if ($r == 'both' || !isset($ret[$r])) return $ret;
		else return $ret[$r];
	}
}